import usbManager from '@ohos.usbManager';
import bundleManager from '@ohos.bundle.bundleManager';
import router from '@system.router';
import CheckEmptyUtils from '../../utils/CheckEmptyUtils.js';

export default {
    data: {
        title: 'World',
        result: 'back',
        deviceInfo: null,
        labelInfo: null,
        index: 0,
        deviceName: ""
    },
    getDeviceList() {
        var deviceList = usbManager.getDevices();
        if (CheckEmptyUtils.isEmptyArr(deviceList)) {
            this.labelInfo = this.$t('device list is empty');
            return;
        }

        let name = 1
        if (deviceList.length - 1 < this.index) {
            name = this.index + 1;
            this.result = "error ： 未插入设备" + name;
            console.info("usb device size is : " + deviceList.length);
            console.info("usb current index is : " + this.index);
        }
        this.deviceInfo = deviceList[this.index];
        console.info("usb current device: " + JSON.stringify(this.deviceInfo));
        this.deviceName = "设备" + name + " : " + this.deviceInfo.name;
        var devices = JSON.stringify(this.deviceInfo);
        console.info("get devices result : " + devices);
        this.labelInfo = devices;
    },
    onInit() {
        this.index = this.indexParam;
        console.info("usb device index : " + this.index);
        this.getDeviceList();
    },
    requestRight() {
        if (this.deviceInfo == null) {
            console.info("usb no device attached");
            this.result = "usb no device attached";
            return;
        }
        var name = this.deviceInfo.name;
        try {
            usbManager.requestRight(name).then(hasRight=>{
                console.info("usb device request right result: " + hasRight);
                this.result = "device request right result: " + hasRight;
            }).catch(error=>{
                console.info("usb device request right failed : " + error);
                this.result = "device request right failed : " + error;
            });
        } catch (err) {
            console.info('usb requestRight-addRight catch err code: ' + err.code + ' message: ' + err.message);
            this.result = err.code + ": " + err.message;
        }

    },
    hasRight() {
        if (this.deviceInfo == null) {
            console.info("usb no device attached");
            this.result = "usb no device attached";
            return;
        }
        var name = this.deviceInfo.name;

        var isRight = usbManager.hasRight(name);
        console.info("device has right : " + isRight);
        this.result = "device has right : " + isRight;
    },
    removeRight() {
        if (this.deviceInfo == null) {
            console.info("usb no device attached");
            this.result = "usb no device attached";
            return;
        }
        var name = this.deviceInfo.name;
        var reRight = usbManager.removeRight(name);
        console.info("usb device remove right result: " + reRight);
        var isRight = usbManager.hasRight(name);
        this.result = "device remove right result: " + reRight + ", device has right : " + isRight;

//        usb.removeRight(name).then(reRight => {
//            console.info("usb device remove right result: " + reRight);
//            var isRight = usb.hasRight(name);
//            this.result = "device remove right result: " + reRight + ", device has right : " + isRight;
//        }).catch(error => {
//            console.info("usb device remove right failed : " + error);
//            this.result = "device remove right failed : " + error;
//        });

    },
    //测试非系统app调用系统api:addRight
    async addDeviceAccessRight() {
        if (this.deviceInfo == null) {
            console.info("usb no device attached");
            this.result = "usb no device attached";
            return;
        }
        var name = this.deviceInfo.name;
        // try {
        //     console.info("addRight begin");
        //     usbManager.addRight("com.usb.right",name);
        // } catch (err) {
        //     console.info('usb addRight catch err code: ' + err.code + ' message: ' + err.message);
        //     this.result = 'usb addRight catch err code: ' + err.code + ' message: ' + err.message;
        // }
        try {
            let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION;
            await bundleManager.getBundleInfoForSelf(bundleFlags).then((bundleInfo) => {
                console.info('testTag', 'getBundleInfoForSelf successfully. Data: %{public}s', JSON.stringify(bundleInfo));
                let tokenId = bundleInfo.appInfo.accessTokenId;
                tokenId = tokenId.toString();
                console.info("addDeviceAccessRight begin");
                let rightResult = usbManager.addDeviceAccessRight(tokenId, name);
                console.log(`addDeviceAccessRight result ： ` + rightResult);
                if (rightResult) {
                    console.log(`Succeed in adding right`);
                    this.result = 'usb addDeviceAccessRight success ,ret : ' + rightResult;
                } else {
                    console.log(`Failed in adding right`);
                    this.result = 'usb addDeviceAccessRight fail ,ret : ' + rightResult;
                }
            }).catch(err => {
                console.error('testTag getBundleInfoForSelf failed' );
                this.result = 'usb getBundleInfoForSelf catch err code: ' + err.code + ' message: ' + err.message;
            });
        } catch (err) {
            console.info('usb addDeviceAccessRight catch err code: ' + err.code + ' message: ' + err.message);
            this.result = 'usb addDeviceAccessRight catch err code: ' + err.code + ' message: ' + err.message;
        }
    },

    onBackPress() {
        router.back();
    }
}
